# -*- coding: utf-8 -*-
import sys,  string
from PyQt4 import QtCore, QtGui
from Ui_ALMConnect_ui import Ui_MainWindow

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

class StartQt4(QtGui.QMainWindow):
        def __init__(self, parent=None):
                QtGui.QWidget.__init__(self, parent)
                self.ui = Ui_MainWindow()
                self.ui.setupUi(self)
                self.filename = None
                self.loadExcel()
                #QtCore.QObject.connect(self.ui.listWidget,QtCore.SIGNAL(_fromUtf8("itemActivated(QListWidgetItem)")), self.itemTolistWidget2)
                #QtCore.QObject.connect(self.ui.ComboExcel, QtCore.SIGNAL(_fromUtf8("clicked()")), self.loadExcel)
                
                #QtCore.QObject.connect(self.ui.pushButton, QtCore.SIGNAL(_fromUtf8("clicked()")), self.listWidgetTo2)
                QtCore.QObject.connect(self.ui.pushButton_2, QtCore.SIGNAL(_fromUtf8("clicked()")), self.listWidgetTo2)
                QtCore.QObject.connect(self.ui.pushButton_3, QtCore.SIGNAL(_fromUtf8("clicked()")), self.itemTolistWidget2)
                QtCore.QObject.connect(self.ui.btnCreateExcel,  QtCore.SIGNAL(_fromUtf8("clicked()")),  self.copyWorkbook)
                #QtCore.QObject.connect(self.ui.pushButton, QtCore.SIGNAL(_fromUtf8("clicked()")), self.ui.listWidget.clear)
                #QtCore.QObject.connect(self.ui.listWidget,QtCore.SIGNAL(_fromUtf8("currentTextChanged(QString)")), self.ui.listWidget_2.addItem)
                #QtCore.QObject.connect(self.listWidget, QtCore.SIGNAL(_fromUtf8("currentTextChanged(QString)")), self.label.setText)
                #QtCore.QObject.connect(self.ui.button_save,QtCore.SIGNAL("clicked()"), self.file_save)
                #QtCore.QObject.connect(self.ui.editor_window,QtCore.SIGNAL("textChanged()"), self.enable_save)
        
        def itemTolistWidget2(self):
            itemslist = self.ui.listWidget_2.selectedItems()
            for item in itemslist:
                #self.ui.listWidget_2.row (item)
                self.ui.listWidget_2.takeItem (self.ui.listWidget_2.row (item))
        
        def listWidgetTo2(self):
            itemslist = self.ui.listWidget.selectedItems()
            for item in itemslist:
                 self.ui.listWidget_2.addItem(item.text())
                
        
        def loadExcel(self):
            self.ui.listWidget.clear()
            try:
                import xlrd
            except AttributeError:
                print "error"
            data = xlrd.open_workbook(u"C:\Auto\QTP2\Plan and Data\ES_Compass_D4 with ES flow.xlsx")
            table = data.sheet_by_name(u"WorkFlow")
            nrows = table.nrows
            #print nrows
            for i in range(nrows-2):
                if table.cell(i+2,3).value is not None or table.cell(i+2,3).value is not "":
                    #print table.cell(i+2,3).value,  type(table.cell(i+2,3).value)
                    self.ui.listWidget.addItem (table.cell(i+2,3).value)
        
        def CreateExcel(self):
            try:
                import xlwt
            except AttributeError:
                print "import xlwt error!"
            wbk = xlwt.Workbook()
            #style = xlwt.XFStyle()
            sheet = wbk.add_sheet('WorkFlow', cell_overwrite_ok=True)
            sheet.write(0,3,'Testcase Path')
            itemslist = self.ui.listWidget_2.selectedItems()
            print type(itemslist), list(enumerate(itemslist))
            for index, item in enumerate(itemslist):
                #self.ui.listWidget_2.row (item)
                #print index,  item.text(), type(item.text())
                sheet.write(int(index+1),3, str(item.text()))
            wbk.save('test.xls')
            print "Done!"
            
        def copyWorkbook(self):
            try:
                from  xlCpWkbook import cpXF
            except AttributeError:
                print "import xlCpWkbook error!"
            cpXF("test3.xls", "test4.xls")
            
#        def file_dialog(self): 
#                fd = QtGui.QFileDialog(self)
#                self.filename = fd.getOpenFileName()
#                from os.path import isfile
#                if isfile(self.filename):
#                        import codecs
#                        s = codecs.open(self.filename,'r','utf-8').read()
#                        self.ui.editor_window.setPlainText(s)
#                        # inserting text emits textChanged() so we disable the button :)
#                        self.ui.button_save.setEnabled(False)
#        def enable_save(self):
#                self.ui.button_save.setEnabled(True)
#        def file_save(self):
#                from os.path import isfile
#                if isfile(self.filename):
#                        import codecs
#                        s = codecs.open(self.filename,'w','utf-8')
#                        s.write(unicode(self.ui.editor_window.toPlainText()))
#                        s.close()
#                        self.ui.button_save.setEnabled(False)

if __name__ == "__main__":
        app = QtGui.QApplication(sys.argv)
        myapp = StartQt4()
        myapp.show()
        sys.exit(app.exec_())

